package com.liarjo.mywebsiteapi.admin.controller.register;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.liarjo.mywebsiteapi.admin.config.AppConstants;
import com.liarjo.mywebsiteapi.admin.pojo.User;
import com.liarjo.mywebsiteapi.admin.pojo.UserBaseMapper;

@Service
public class RegisterService {
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private UserBaseMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    public RegisterResult register(RegisterRequest registerRequest) {
        if (registerMapper.existsByUsername(registerRequest.getUserName()) > 0) {
            throw new RuntimeException("user name already exists");
        }
        User user = new User();
        user.setUserName(registerRequest.getUserName());
        user.setPassword(passwordEncoder.encode(registerRequest.getPassword()));
        user.setRole(AppConstants.ROLE_USER);
        userMapper.insert(user);
        return null;
    }
}
